package com.yzf.demo.demo.DesignPattren.SingletonPattern.staticInnersingleton;

import java.lang.reflect.Constructor;

/**
 * Created by 于占峰 on 2020/2/27/027.
 * 模拟反射机制调用单例对象的私有构造器破坏单例模式
 */
public class StaticInnerSingletonTest {

    public static void main(String[] args) {
        try {
            Class<?> clazz = StaticInnerSingleton.class;
            //通过反射机制获得私有的构造方法
            Constructor constructor = clazz.getDeclaredConstructor(null);
            //强制访问
            constructor.setAccessible(true);
            //创建两次实例 验证结果
            Object staticInnerSingleton1 = constructor.newInstance();
            Object staticInnerSingleton2 = constructor.newInstance();
            System.out.println(staticInnerSingleton1 == staticInnerSingleton2);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
